<?php 
$orderIds = $this->getRequest()->getPost('order_ids');
$_coreHelper = $this->helper('core');
$orderItems = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('order_id',array('in'=>$orderIds));
$products = array();
foreach($orderItems as $item)
{
	$order = Mage::getModel('sales/order')->load($item->getOrderId());
	if($order->getStatus()=='canceled') continue;
	if(isset($products[$item->getSku()]))
	{
		$products[$item->getSku()]['qty'] = $products[$item->getSku()]['qty'] + $item->getQtyOrdered();
	}
	else
	{
		/*$product = Mage::getModel('catalog/product')->load($item->getProductId());
		$products[$item->getSku()] = array('id'=>$item->getProductId(),'qty'=>$item->getQtyOrdered());*/
		$product = Mage::getModel('catalog/product')->setStoreId($item->getStoreId())->load($item->getProductId());
		$products[$item->getSku()] = array(
			'name'=>$product->getName(),
			'sku'=>$item->getSku(),
			'id'=>$item->getProductId(),
			'qty'=>$item->getQtyOrdered(),
			'unit'=>$product->getAttributeText('unit'),
			'location'=>$product->getLocation()
		);
	}
}

$whProducts = array();
$nwhProducts = array();
foreach($products as $p)
{
	if($p['location'])
	{
		$whProducts[] = $p;
	}
	else
	{
		$nwhProducts[] = $p;
	}
}
usort($whProducts, function($item1, $item2){
	if ($item1['location'] == $item2['location']) return 0;
    return $item1['location'] > $item2['location'] ? -1 : 1;
});
foreach($nwhProducts as $p)
{
	array_unshift($whProducts, $p);
}

?>
<style>
* {font-family: "微软雅黑"}
body {color:#000000;font-weight:bold;font:12px/1.5em Arial, Helvetica, sans-serif}
th,td { color:#000000 !important;  border-color:#000000 !important; }
</style>
<div class="print">
	<div class="order-print">
		<div class="head" style="text-align: center !important;font-weight:bold;font-size:20pt;">拣货单</div>
		<div class="head"> <div class="fr">打印日期：<?php echo date("Y年m月d日 H:i",time()+3600*8);?> <a href="javascript:window.print();" class="actions"> 打印</a></div></div>
		<table class="data-table">
			<tr>
				<td>商品名</td>
				<td width="15%">SKU</td>
				<td width="12%">订购数量</td>
				<td width="15%">库位</td>
			</tr>
			<?php foreach($whProducts as $item):?>
				<tr>
					<td><?php echo $item['name'];?></td>
					<td><?php echo $item['sku'];?></td>
					<td><?php echo intval($item['qty']);?></td>
					<td><?php echo $item['location'];?></td>
				</tr>
			<?php endforeach;?>
		</table>
	</div>
</div>